package com.cdtye.shuoh.config.netty;


import com.cdtye.shuoh.util.text.DataConverter;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class MyDecoder extends ByteToMessageDecoder {

    private final Logger logger = LoggerFactory.getLogger(MyDecoder.class);

    @Override
     protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        // 复制内容到字节数组bytes
        buf.readBytes(bytes);
        // 将接收到的数据转为字符串，此字符串就是客户端发送的字符串
        String receiveStr = DataConverter.receiveHexToString(bytes);
        logger.info("接送到消息"+receiveStr);
        out.add(receiveStr);
        }
}

